package com.liy.framework.common.office.pdf;

import java.io.*;
import java.util.*;
import com.artofsolving.jodconverter.openoffice.converter.*;
import java.net.*;
import java.text.*;
import com.artofsolving.jodconverter.openoffice.connection.*;
import com.artofsolving.jodconverter.*;

public class OfficeToPDF
{
    public static void main(final String[] args) {
        officeToPDF("C:\\Temp\\doc\\2.xls", "C:\\Temp\\doc\\3.pdf");
    }
    
    public static boolean officeToPDF(final String sourceFile, final String destFile) {
        try {
            final File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                return false;
            }
            final File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            if (outputFile.exists()) {
                outputFile.delete();
            }
            final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            final OpenOfficeConnection connection = (OpenOfficeConnection)new SocketOpenOfficeConnection("10.10.10.129", 8100);
            connection.connect();
            System.out.println("\u8fde\u63a5\u65f6\u95f4:" + df.format(new Date()));
            final DocumentConverter converter = (DocumentConverter)new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(inputFile, outputFile);
            System.out.println("\u8f6c\u6362\u65f6\u95f4:" + df.format(new Date()));
            connection.disconnect();
            return true;
        }
        catch (ConnectException e) {
            e.printStackTrace();
            System.err.println("openOffice\u8fde\u63a5\u5931\u8d25\uff01\u8bf7\u68c0\u67e5IP,\u7aef\u53e3");
        }
        catch (Exception e2) {
            e2.printStackTrace();
        }
        return false;
    }
}
